package com.cyt.wx.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.cyt.base.model.dto.ReturnResult;
import com.cyt.base.util.BaseConstant;
import com.cyt.base.util.DictCode;
import com.cyt.base.util.ErrorCode;
import com.cyt.sys.model.po.SysUser;
import com.cyt.sys.service.impl.BaseServiceImpl;
import com.cyt.wx.dao.WxApplyDao;
import com.cyt.wx.model.po.WxApply;
import com.cyt.wx.model.vo.WxApplyVo;
import com.cyt.wx.service.WxApplyService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * 应用管理-应用信息Service层实现类
 *
 * @author rong yuan
 * @date 2019-8-16 8:45:15
 */
@Service("wxApplyService")
public class WxApplyServiceImpl extends BaseServiceImpl<WxApply> implements WxApplyService {

    @Resource
    private WxApplyDao wxApplyDao;

    @Autowired
    public void setBaseDao(WxApplyDao wxApplyDao) {
        this.baseDao = wxApplyDao;
    }

    /**
     * 分页查询
     *
     * @param wxApplyVo 接受参数对象
     * @param pageNum   分页数
     * @param pageSize  每页显示条数
     * @return ReturnResult
     * @author rong yuan
     * @date 2019-8-16 8:45:15
     */
    @Override
    public ReturnResult page(WxApplyVo wxApplyVo, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        PageHelper.orderBy(" create_time desc");
        List<WxApplyVo> list = wxApplyDao.selectByCondition(wxApplyVo);
        PageInfo<WxApplyVo> page = new PageInfo<>(list);
        return new ReturnResult(page.getList(), page.getTotal(), page.isHasNextPage());
    }
    /**
     * list查询
     *
     * @param wxApplyVo 接受参数对象
     * @return List<WxApply>
     * @author shengQiang yu
     * @date 2019-8-16 08:50
     */
    @Override
    public List<WxApply> selectListByCondition(WxApplyVo wxApplyVo) {
        return wxApplyDao.selectListByCondition(wxApplyVo);
    }
    /**
     * list查询
     *
     * @param wxApplyVo 接受参数对象
     * @return List<WxApply>
     * @author rong yuan
     * @date 2019-8-16 08:50
     */
    @Override
    public List<WxApply> listExample(WxApplyVo wxApplyVo) {
        Example example = new Example(WxApply.class);
        Example.Criteria criteria = example.createCriteria();
        if (StringUtils.isNotBlank(wxApplyVo.getUserId())) {
            criteria.andEqualTo("userId", wxApplyVo.getUserId());
        }
        if (StringUtils.isNotBlank(wxApplyVo.getApplyUseId())) {
            criteria.andEqualTo("applyUseId", wxApplyVo.getApplyUseId());
        }
        criteria.andEqualTo("removeTag", DictCode.BASE_REMOVE_TAG_NORMAL);
        return wxApplyDao.selectByExample(example);
    }

    /**
     * 对象查询
     *
     * @param wxApplyVo 接受参数对象
     * @return WxApplyVo
     * @author rong yuan
     * @date 2019-8-16 09:14
     */
    @Override
    public WxApplyVo selectOne(WxApplyVo wxApplyVo) {
        return wxApplyDao.selectOneByCondition(wxApplyVo);
    }

    /**
     * 添加或更新
     *
     * @param wxApplyVo 参数接受对象
     * @param sysUser   当前用户
     * @return ReturnResult
     * @author rong yuan
     * @date 2019-8-16 8:45:15
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public ReturnResult addOrUpdate(WxApplyVo wxApplyVo, SysUser sysUser) {
        if (StringUtils.isBlank(wxApplyVo.getId())) {
            List<WxApply> list = listExample(new WxApplyVo(sysUser.getId()));
            String accountId="";
            Boolean flage=false;
            if (list != null && list.size() > 0) {
                for(WxApply wxApply:list){
                    if(wxApply.getApplyUseId().equals(wxApplyVo.getApplyUseId())){
                        flage=true;
                        break;
                    }
                    if(StrUtil.isNotEmpty(wxApply.getAccountId())){
                        accountId=wxApply.getAccountId();
                    }
                }
            }
            if(flage){
                return new ReturnResult(ErrorCode.CODE_100, "您已经创建了应用[" + wxApplyVo.getName() + "]!");
            }
            WxApply wxApply = new WxApply(sysUser);
            wxApply.setUserId(sysUser.getId());
            wxApply.setName(wxApplyVo.getName());
            wxApply.setCoverImage(wxApplyVo.getCoverImage());
            wxApply.setApplyUseId(wxApplyVo.getApplyUseId());
            wxApply.setAccountId(accountId);
            wxApply.setStatus(wxApplyVo.getStatus());
            wxApply.setUseDay(DateUtil.offsetDay(wxApply.getCreateTime(), Integer.parseInt(wxApplyVo.getUseDay())));
            wxApplyDao.insert(wxApply);
            return new ReturnResult(BaseConstant.DATABASE_SUCCEED_INSERT);
        } else {
            WxApply wxApply = wxApplyDao.selectByPrimaryKey(wxApplyVo.getId());
            wxApply.setName(wxApplyVo.getName());
            wxApply.setCoverImage(wxApplyVo.getCoverImage());
            wxApply.setStatus(wxApplyVo.getStatus());
            if (wxApply.getUseDay().getTime() >= System.currentTimeMillis()) {
                wxApply.setUseDay(DateUtil.offsetDay(wxApply.getUseDay(), Integer.parseInt(wxApplyVo.getUseDay())));
            } else {
                wxApply.setUseDay(DateUtil.offsetDay(new Date(), Integer.parseInt(wxApplyVo.getUseDay())));
            }
            wxApply.setUpdateTime(new Date());
            wxApply.setUpdateUser(sysUser.getId());
            int num = wxApplyDao.updateByPrimaryKey(wxApply);
            if (num > 0) {
                if (StringUtils.isNotBlank(wxApplyVo.getAccountId())) {
                    //同时改变当前用户下所有应用的
                    Example example = new Example(WxApply.class);
                    Example.Criteria criteria = example.createCriteria();
                    criteria.andEqualTo("userId", wxApply.getUserId());
                    wxApplyDao.updateByExampleSelective(new WxApply(wxApplyVo.getAccountId()), example);
                }
            }
            return new ReturnResult(BaseConstant.DATABASE_SUCCEED_UPDATE);
        }
    }

    /**
     * 根据条件逻辑删除
     *
     * @param wxApplyVo 参数接受对象
     * @param sysUser   当前用户
     * @return ReturnResult
     * @author rong yuan
     * @date 2019-8-16 8:45:15
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public ReturnResult remove(WxApplyVo wxApplyVo, SysUser sysUser) {
        //要修改的字段
        WxApply wxApply = new WxApply();
        wxApply.setRemoveTag(DictCode.BASE_REMOVE_TAG_DELETE);
        wxApply.setUpdateUser(sysUser.getId());
        wxApply.setUpdateTime(new Date());
        //筛选条件
        Example example = new Example(WxApply.class);
        Example.Criteria criteria = example.createCriteria();
        if (StringUtils.isNotBlank(wxApplyVo.getId())) {
            criteria.andIn("id", Arrays.asList(wxApplyVo.getId().split(",")));
        }
        criteria.andEqualTo("removeTag", DictCode.BASE_REMOVE_TAG_NORMAL);
        wxApplyDao.updateByExampleSelective(wxApply, example);
        return new ReturnResult(ErrorCode.CODE_0, BaseConstant.DATABASE_SUCCEED_DEL);
    }

    /**
     * 根据条件物理删除
     *
     * @param wxApplyVo 参数接受对象
     * @param sysUser   当前用户
     * @return ReturnResult
     * @author rong yuan
     * @date 2019-8-16 8:45:15
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public ReturnResult delete(WxApplyVo wxApplyVo, SysUser sysUser) {
        //筛选条件
        Example example = new Example(WxApply.class);
        Example.Criteria criteria = example.createCriteria();
        if (StringUtils.isNotBlank(wxApplyVo.getId())) {
            criteria.andIn("id", Arrays.asList(wxApplyVo.getId().split(",")));
        }
        wxApplyDao.deleteByExample(example);
        return new ReturnResult(ErrorCode.CODE_0, BaseConstant.DATABASE_SUCCEED_DEL);
    }
}